import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/server/Manager.vue'
import store from "../store";


Vue.use(VueRouter)

const routes = [
  /*{
    path: '/',
    name: 'Manager',
    component: Home,
    redirect: "/home",
    children:[
      { path: 'user', name: '用户管理', component: () => import('../views/server/User')},
      { path: 'home', name: '首页', component: () => import('../views/server/Home')},
      { path: 'goods', name: '商品', component: () => import('../views/server/Goods')},
      { path: 'person', name: '个人信息', component: () => import('../views/server/Person')},
      { path: 'file', name: '文件上传', component: () => import('../views/server/File')},
      { path: 'role', name: '角色管理', component: () => import('../views/server/Role')},
      { path: 'menu', name: '菜单管理', component: () => import('../views/server/Menu')},
    ]
  },*/

  {
    path: '/login',
    name: '登录',
    component: () => import('../views/server/Login'),

  },
  {
    path: '/register',
    name: '注册',
    component: () => import('../views/server/Register')
  },
  {
    path: '*',
    name: "错误",
    component: () => import('../views/error')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export const setRouters = () =>{
  const storeMenu = localStorage.getItem("menus");
  const user = JSON.parse(localStorage.getItem("user"));
  console.log(storeMenu)
  if (storeMenu && user.role !== 'ROLE_CUSTOMER'){
    //拼装动态路由
    const menageRoute = {path: '/', name: '首页', component: () => import('../views/server/Manager'),redirect: "/home", children:[] }
    const menus = JSON.parse(storeMenu);
    menus.forEach(item =>{
      if (item.path){ // path不为空的时候直接设置路由
        let itemMenu = {path: item.path.replace("/",""), name: item.name, component: () => import('../views/server/'+item.pagePath+'.vue')}
        menageRoute.children.push(itemMenu)
      } else if(item.children.length) { //如果path为空，children不为空
        item.children.forEach(item =>{
          if (item.path){ // 如果不为空
            let itemMenu = {path: item.path.replace("/",""), name: item.name, component: () => import('../views/server/'+item.pagePath+'.vue')}
            menageRoute.children.push(itemMenu)
          }
        })
      }
    })
    //获取当前的动态数组
    const currentRouters = router.getRoutes().map(v => v.name)
    if (! currentRouters.includes('首页')){
      // 动态添加现在的路由对象去
      router.addRoute(menageRoute)
      console.log(menageRoute)
    }
  }
}
setRouters();

router.beforeEach(
    (to, from, next) => {
      localStorage.setItem("currentPathName",to.name)
      store.commit("setPath")
      const user = JSON.parse(localStorage.getItem("user"));
      next();
    }
)
// 解决报错
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

export default router
